package ${corePackageStr};

import com.differ.jackyun.frame.base.enums.CommonResponseCodesEnum;
import com.differ.jackyun.frame.base.util.MemberInfoUtils;
import com.differ.jackyun.frame.business.core.ICommonCore;
import com.differ.jackyun.frame.business.dao.ICommonDao;
import com.differ.jackyun.framework.component.basic.interceptor.ServiceException;
import com.differ.jackyun.framework.component.basic.util.IdWorkerUtil;
import ${daoPackageStr}.${className};
import ${filterPackageStr}.${filterName};
import ${filterPackageStr}.${resultName};
import ${entityPackageStr}.${entityClassName};
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * ${entityDesc} - CORE
 *
 * @author ${author}
 * @since ${time}
 */
@Component
public class ${coreName} implements ICommonCore<${filterName}, ${entityClassName}, ${resultName}> {
    @Autowired
    private ${className} ${classNameLow};

    /**
     * 获取对应dao
     *
     * @return 对应dao
     */
    @Override
    public ICommonDao getCommonDao() {
        return this.${classNameLow};
    }

    /**
     * 添加操作 - 批量数据校验
     *
     * @param entityList 实体List
     */
    @Override
    public void checkDataForAdd(List<${entityClassName}> entityList) {
        // 先调用（父类）接口的校验
        ICommonCore.super.checkDataForAdd(entityList);

        // 业务代码
        entityList.forEach(e -> {
            // 添加id
            if (Objects.isNull(e.getId()) || e.getId().equals(0L)) {
                // 添加id
                e.setId(IdWorkerUtil.getId());
            }

            // 设置默认值
            e.setCreateId(MemberInfoUtils.getUserId());
            e.setCreator(MemberInfoUtils.getUserName());
            e.setIsDeleted(new Byte("0"));
        });

    }

    /**
     * 修改操作 - 批量数据校验
     *
     * @param entityList 实体List
     */
    @Override
    public void checkDataForUpdate(List<${entityClassName}> entityList) {
        // 先调用（父类）接口的校验
        ICommonCore.super.checkDataForUpdate(entityList);

        // 业务代码
        for (${entityClassName} e : entityList) {
            if (Objects.isNull(e.getId())) {
                throw new ServiceException(CommonResponseCodesEnum.WARN_ID_IS_EMPTY);
            }
        }
    }

    /**
     * 获取实体 - 唯一可能会返回null的接口，调用者需要注意
     *
     * @param id   实体Id
     * @param cols 查询字段例如"id,name,productId"
     * @return 实体
     */
    public ${entityClassName} get(Long id, String cols) {
        // 数据校验
        if (id == null) {
            return null;
        }

        List<${entityClassName}> resultList = this.list(new Long[]{id}, cols);
        // 如果数据为null或者长度等于0
        if (resultList == null || resultList.size() == 0) {
            return null;
        }

        return resultList.get(0);
    }

    /**
     * 批量获取实体
     *
     * @param ids  实体Id数组
     * @param cols 查询字段例如"id,name,productId"
     * @return 实体List
     */
    public List<${entityClassName}> list(Long[] ids, String cols) {
        // 数据校验
        if (ids == null || ids.length == 0) {
            return new ArrayList<>();
        }

        ${filterName} filter = new ${filterName}();
        filter.setIds(ids);
        filter.setStringCols(cols);
        filter.setPageSize(ids.length);
        return this.listByFilter(filter);
    }
}